package cn.wolfcode.web.controller;


import cn.wolfcode.domain.*;
import cn.wolfcode.qo.BusinessQueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.util.FileUploadUtil;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.util.List;

@Controller
@RequestMapping("/business")
public class BusinessController {

    @Autowired
    private IBusinessService businessService;


    @RequirePermission(name = "门店页面",expression = "business:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") BusinessQueryObject qo){
        PageInfo<Business> pageInfo = businessService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "business/list";
    }

    @RequestMapping("/delete")
    @RequirePermission(name = "门店删除",expression = "business:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            businessService.delete(id);
        }
        return new JsonResult();
    }

   /* @Autowired
    private ServletContext servletContext;

    @RequestMapping("/saveOrUpdate")
    @RequirePermission(name = "门店新增/编辑",expression = "business:saveOrUpdate")
    public String saveOrUpdate(Business business, MultipartFile file) throws Exception {
        //检测文件必须写在新增和编辑前面
        //先判断文件是否有上传,有则删除之前的,再保存,保存后获取文件所在路径,再把地址设置到business的文件字段中
        if(file != null && file.getSize() > 0){
            //获取webapp文件的绝对路径,让文件保存到这个文件底下,需要利用Servlet的上下文域(ServletContext)
            String realPath = servletContext.getRealPath("/");

            //删除之前的,根据前端回显的照片路径,判断是否为空,回显不为null则删除文件
            if(StringUtils.hasText(business.getLicenseImg())){
                //注意保存的数据只是相对路径,删除操作需要绝对路径
                FileUploadUtil.deleteFile(realPath+business.getLicenseImg());
            }

            //调用上传文件的工具类,让工具去保存并返回保存后的路径
            String url = FileUploadUtil.uploadFile(file, realPath);
            //把地址保存到对象中
            business.setLicenseImg(url);
        }
        if (business.getId() != null) {
            //如何是更新
            businessService.update(business);
        }else {
            *//*business.setLicenseImg(null);*//*
            businessService.save(business);
        }
        return "redirect:/business/list";
    }*/

    @RequirePermission(name = "门店新增/编辑",expression = "business:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id,Model model){
        if (id != null) {
            Business business = businessService.get(id);
            model.addAttribute("business",business);
        }
        //让浏览器去访问input页面,并把model的数据回显
        return "business/input";
    }

    @RequestMapping("/selectAll")
    @ResponseBody
    public List<Business> selectAll(){
        return businessService.listAll();
    }
}
